package com.hzit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzit.entity.PageResult;
import com.hzit.entity.Student;
import com.hzit.entity.StudentExample;
import com.hzit.mapper.ClassesMapper;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.StudentService;
import com.hzit.util.MybatisUtil;
import com.hzit.vo.StudentVo;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
public class StudentServiceImpl implements StudentService {
	private SqlSessionFactory factory;
	public StudentServiceImpl(){
		try {
			factory = MybatisUtil.getSqlSessionFactory();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//1. 查询所有学生
	@Override
	public List<Student> selectAll() {
		try(SqlSession session = factory.openSession()){
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			return mapper.selectByExample(null);
		}
	}

	//2. 查询单个学生
	@Override
	public Student findById(int sid) {
		try(SqlSession session = factory.openSession()){
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			return mapper.selectByPrimaryKey(sid);
		}
	}

	//3. 添加学生
	@Override
	public void insert(Student stud) {
		try(SqlSession session = factory.openSession(true)){
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			mapper.insert(stud);
		}
	}

	//4. 修改学生
	@Override
	public void update(Student student) {
		try(SqlSession session = factory.openSession(true)){
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			// 会将没有传递的字段修改为null
			// mapper.updateByPrimaryKey(student);
			// 不会将没有传入的字段进行修改，换言之，在修改时会判断传入的字段是否有值！
			mapper.updateByPrimaryKeySelective(student);
		}
	}

	//5. 删除学生
	@Override
	public void deleteById(int sid) {
		try(SqlSession session = factory.openSession(true)) {
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			mapper.deleteByPrimaryKey(sid);
		}
	}

	//6. 条件查询
	@Override
	public List<Student> findByWords(Student student) {
		try(SqlSession session = factory.openSession(true)) {
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			//6.1 定义查询实例
			StudentExample example = new StudentExample();
			//6.2 得到查询实例关联的查询条件对象
			StudentExample.Criteria criteria = example.createCriteria();
			if(student != null){
				if(StrUtil.isNotBlank(student.getSname())){
					criteria.andSnameLike("%" + student.getSname() + "%");
				}
				if(StrUtil.isNotBlank(student.getAddr())){
					criteria.andAddrLike("%" + student.getAddr() + "%");
				}
				if(student.getCid() != null && student.getCid() != 0){
					criteria.andCidEqualTo(student.getCid());
				}
			}
			//6.3 根据条件查询出结果
			List<Student> students = mapper.selectByExample(example);
			//6.4 返回结果
			return students;
		}
	}

	//7. 条件查询带分页
	@Override
	public PageResult<StudentVo> search(int page, int pageSize, Student student) {
		PageHelper.startPage(page,pageSize);
		try(SqlSession session = factory.openSession(true)) {
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
			//7.1 定义查询实例
			StudentExample example = new StudentExample();
			//7.2 得到查询实例关联的查询条件对象
			StudentExample.Criteria criteria = example.createCriteria();
			if(student != null){
				if(StrUtil.isNotBlank(student.getSname())){
					criteria.andSnameLike("%" + student.getSname() + "%");
				}
				if(StrUtil.isNotBlank(student.getAddr())){
					criteria.andAddrLike("%" + student.getAddr() + "%");
				}
				if(student.getCid() != null && student.getCid() != 0){
					criteria.andCidEqualTo(student.getCid());
				}
			}
			//7.3 根据条件查询出结果
			List<Student> students = mapper.selectByExample(example);

			//7.4 返回结果
			Page<Student> studentPage = (Page<Student>) students;
			List<StudentVo> studentVos = new ArrayList<>();
			List<Student> result = studentPage.getResult();
			for (Student st : result) {
				StudentVo vo = new StudentVo();
				BeanUtil.copyProperties(st, vo, true);
				String cname = classesMapper.selectByPrimaryKey(st.getCid()).getCname();
				vo.setCname(cname);
				studentVos.add(vo);
			}

			//7.5 构造返回的PageResult对象
			return new PageResult<>(studentPage.getTotal(),studentVos);
		}

	}
}
